package de.windeln.marketing.campaign.costinterval;

import org.joda.time.DateTime;
import org.joda.time.Days;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import de.windeln.marketing.campaign.costinterval.dao.interfaces.CostIntervalMapper;
import de.windeln.marketing.campaign.costinterval.model.CostintervalClientObject;

@Component
public class CostIntervalValueGenerator {

	@Autowired
	private CostIntervalMapper costIntervalMapper;
	
	public void generate(CostintervalClientObject costInterval) {
		generateTotalCosts(costInterval);
		generateDailyCost(costInterval);
	}
	
	private void generateTotalCosts(CostintervalClientObject costInterval) {
		double costPerVariable = costIntervalMapper.fetchCostPerVariableByCampaignId(costInterval.getCampaignId());
		costInterval.setTotalCosts(costInterval.getFixCosts() + costInterval.getNumberOfVariable() * costPerVariable);	
	}

	private void generateDailyCost(CostintervalClientObject costInterval) {
	        int daysDifference = 1 + Days.daysBetween(new DateTime(costInterval.getCostStart()), new DateTime(costInterval.getCostEnd())).getDays();
	        double campaignDailyCost = costInterval.getTotalCosts() / daysDifference;
	        double campaignDailyCost3Digit = (double)Math.round(campaignDailyCost * 1000) / 1000;
	        costInterval.setDailyCost(campaignDailyCost3Digit);	
	}	

}
